
<template>
  <section class="main">
    <input id="toggle-all" @click="allCheckout" class="toggle-all" type="checkbox" :checked="isCheckoutAll"/>
    <ul class="todo-list" v-for="item in list" :key="item.id">
      <li :class="{'completed':item.done}" >
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done" @click="checkoutChange(item.id,$event)"/>
          <label>{{item.name}}</label>
          <button class="destroy" @click="destroyEvent(item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps(['list'])
// const emits = defineEmits([])
const emits = defineEmits(['updateCheckoutedEvent','destroyEvent'])
// 修改事件状态
const checkoutChange = (id,e)=> emits('updateCheckoutedEvent',{id,checked:e.target.checked})
// 删除事件
const destroyEvent = id => emits('destroyEvent',id)
// 全选反选
// 缺陷写法当数组为空时every会直接返回一个true值,导致没数据时全选框会默认选中
// const isCheckoutAll = computed(()=>props.list.every(item=>item.done))
const isCheckoutAll = computed(()=>{
  if(props.list.length===0){
    return false
  }
  return props.list.every(item=>item.done)
})
// console.log(isCheckoutAll);
const allCheckout = e => emits('hAllCheckout',e.target.checked)
</script>